package com.sh.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.sh.dao.OilDao;
import com.sh.entity.Oil;
import com.sh.entity.User;
import com.sh.service.OilService;
import com.sh.util.DateUtil;

@Service
public class OilServiceImpl implements OilService{
	@Autowired
	private OilDao oilDao;

	@Override
	public List<Oil> listOil(Map<String, Object> conMap,User user,String sysTime) {
		// TODO Auto-generated method stub 

		return oilDao.listOil(conMap);
	}

	//上传 excel文件,并且到DB
	@Override
	public String saveUpload(List<Oil> checkList, User user, String sysTime) {
		// TODO Auto-generated method stub
		oilDao.saveUpload(checkList,user,sysTime);

		return "";
	}
	//查询数量
	@Override
	public int listOilCount(Map<String, Object> conMap) {
		// TODO Auto-generated method stub
		return oilDao.listOilCount(conMap);
	}

	//批量删除
	@Override
	public void deleteOils(String[] ids) {
		// TODO Auto-generated method stub
		oilDao.deleteOils(ids);
	}
	/**
	 * 新增 / 修改 保存数据
	 * */
	@Override
	public void saveOils(Oil oil, User user) {
		// TODO Auto-generated method stub
		oilDao.saveOils(oil,user);
	}

	//判断 数据是不是已经存在
	//根据 【日期】【车号】【客户名】【金额】判断
	@Override
	public boolean isOilExist(Oil oil) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//修改的时候判断 数据有么有存在
		if(!StringUtils.isEmpty(oil.getId())){
			int count = oilDao.isOilExistForUpdate(oil);
			flag = (count==0?false:true);
		}else{
			int count = oilDao.isOilExistForAdd(oil);
			flag = (count==0?false:true);
		}

		return flag;
	}

	//验证 需要删除的ID对应的数据 是不是 已经 结转
	@Override
	public List<String> listYearByIds(String[] ids) {
		// TODO Auto-generated method stub
		List<String> rtnList = new ArrayList<>();
		List list = oilDao.listYearByIds(ids);
		if(list!=null &&
				list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				String date =
						(String)map.get("oil_date");
				if(DateUtil.checkDateFormat(date, "yyyy-MM-dd")){
					if(!rtnList.contains(date.split("-")[0])){
						rtnList.add(date.split("-")[0]);
					}
				}
			}
			return rtnList;

		}
		return null;
	}


}
